﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.IO;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using System.Xml.Linq;
using System.Reflection.Metadata;

namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// 物流计划OA消息推送
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TMSShipPlanOASendController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string con_zh;
        public readonly string con_qh;
        public readonly string con_yb;


        public TMSShipPlanOASendController(ILogger<TMS_OaMessagePushInfoRequestDto> logger, IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration, ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
            con = _configuration.GetConnectionString("DDM_DbConnection_ZH");
            con_zh = _configuration.GetConnectionString("WMSDbConnection_ZH");
            con_qh = _configuration.GetConnectionString("WMSDbConnection");
            con_yb = _configuration.GetConnectionString("WMSDbConnection_YB");
            _logger = loggerFactory.CreateLogger<TMStoWMSAddShipPlanController>();
        }

        /// <summary>
        /// 物流计划OA消息推送
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostShipPlanToOA([FromBody] TMS_OaMessagePushInfoRequestDto strparm)
        {
            string strMsg = "";
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            TMS_OaMessagePushInfoResponseDto re = new TMS_OaMessagePushInfoResponseDto();
            string logistics_plan_no = strparm.logistics_plan_no;
            string in_con = "";//目标WMS数据库
            try
            {
                if (logistics_plan_no == "")
                {
                    strMsg = "logistics_plan_no must be not null";
                }
               

                if (strMsg == "")
                {
                  
                    Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
                    dic.Add("logistics_plan_no", logistics_plan_no);//查询对应物流计划
                    DataTable wl_dt = MethodHelp.GetTableInfo("TMS0012", dic, con);//获取物流计划信息
                    if (wl_dt.Rows.Count > 0)
                    {
                        foreach (DataRow item in wl_dt.Rows)
                        {
                            string userlist = "";
                            string jd = item["factory_code"].ToString().Replace("\"", "").Replace("[", "").Replace("]","");
                            string jdname = "";

                            if (item["factory_code"].ToString().Contains("zh"))
                            {
                                jdname = "金湾高景";

                            }
                            else if (item["factory_code"].ToString().Contains("qh"))
                            {
                                jdname = "青海高景";
                            }
                            else if (item["factory_code"].ToString().Contains("sc"))
                            {
                                jdname = "宜宾高景";
                            }
                            else if (item["factory_code"].ToString().Contains("ZN"))
                            {
                                jdname = "宜宾高臻";
                            }
                            else if (item["factory_code"].ToString().Contains("gz"))
                            {
                                jdname = "广州高景";
                            }
                            else if (item["factory_code"].ToString().Contains("gd"))
                            {
                                jdname = "广东高景";
                            }
                            else
                            {
                                jdname = "金湾高景";
                            }

                            dic = new Dictionary<string, string>();
                            dic.Add("factory_code", jd);
                            dic.Add("massage_type", "物流计划");
                            DataTable dt_uselist = MethodHelp.GetTableInfo("TMS0020", dic, con);
                            if (dt_uselist.Rows.Count > 0)
                            {
                                foreach (DataRow row in dt_uselist.Rows)
                                {
                                    userlist = userlist + row["job_no"].ToString() + ",";
                                }
                            }
                            string struser = userlist.TrimEnd(',');

                            string Sendinfo = $" 计划发货日期:" + item["ship_date"].ToString() + "     <br/>物流下发日期: " + item["w_create_date"].ToString() + "     <br/>创建人: " + item["w_creator"].ToString() + "  <br/> 物料编码: " + item["material_code"].ToString() + "      <br/>数量: " + item["p_material_amount"].ToString() + "       <br/> 产品描述:" + item["material_name"].ToString() + "       <br/> 客户备注:"+ item["remarks"].ToString();
                            string title = $"提醒类型:物流计划下发" + "  <br/> 所属基地: " + jdname + "  <br/> 商务计划号: " + item["plan_code"].ToString()+ "  <br/> 物流计划号: " + item["w_plan_code"].ToString() + "   <br/>客户： "+ item["customer_name"].ToString();
                            dic = new Dictionary<string, string>();
                            dic.Add("code", "595");
                            dic.Add("context", Sendinfo);
                            dic.Add("creater", "");
                            dic.Add("linkMobileUrl", "");
                            dic.Add("linkUrl", "");
                            dic.Add("title", title);
                            dic.Add("workCodeList", struser);

                            strMsg = MethodHelp.WB_OaMessagePush(dic, con);
                        }                                               
                    }
                    else
                    {
                        strMsg = "logistics_plan_no=<" + logistics_plan_no + "> info  not find";
                    }                  
                }
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

            if (strMsg == "")
            {
                re.code = 0;
                re.message = "";
            }
            else
            {
                re.code = 500;
                re.message = strMsg;

            }


            _logger.LogInformation(JsonConvert.SerializeObject(re));
            return new JsonResult(re);

        }


    }
}
